|
1
|
|
|
/**
|
|
2
|
|
|
* @author Jinzulen
|
|
3
|
|
|
* @license Apache 2.0
|
|
4
|
|
|
*
|
|
5
|
|
|
* TenorJS - Lightweight NodeJS wrapper around the Tenor.com API.
|
|
6
|
|
|
*/
|
|
7
|
|
|
const FS = require("fs"),
|
|
8
|
|
|
Colors = require("colors");
|
|
9
|
|
|
|
|
10
|
|
|
exports.client = function (Credentials)
|
|
11
|
|
|
{
|
|
12
|
|
|
const Filters = ["off", "low", "medium", "high"],
|
|
13
|
|
|
MediaFilters = ["basic", "minimal"];
|
|
14
|
|
|
|
|
15
|
|
|
/**
|
|
16
|
|
|
* Credentials checks.
|
|
17
|
|
|
*/
|
|
18
|
|
|
if (!Credentials.Key || !Credentials.Locale || !Credentials.Filter)
|
|
19
|
|
|
{
|
|
20
|
|
|
throw new Error ("Client configuration is not complete; please ensure all configuration parameters are satisfied (Key, Locale, Filter).");
|
|
21
|
|
|
}
|
|
22
|
|
|
|
|
23
|
|
|
if (!Filters.includes(Credentials.Filter.toLowerCase()))
|
|
24
|
|
|
{
|
|
25
|
|
|
throw new Error ("Content filter level has to be one of these options: off, low, medium, high.");
|
|
26
|
|
|
}
|
|
27
|
|
|
|
|
28
|
|
|
if (Credentials.MediaFilter)
|
|
29
|
|
|
{
|
|
30
|
|
|
if (!MediaFilters.includes(Credentials.MediaFilter.toLowerCase()))
|
|
31
|
|
|
{
|
|
32
|
|
|
throw new Error ("Media content filter has to be one of these options: basic, minimal.");
|
|
33
|
|
|
}
|
|
34
|
|
|
} else if (!Credentials.MediaFilter) {
|
|
35
|
|
|
Credentials.MediaFilter = "minimal";
|
|
36
|
|
|
}
|
|
37
|
|
|
|
|
38
|
|
|
if (!Credentials.DateFormat) Credentials.DateFormat = "D/MM/YYYY - H:mm:ss A";
|
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
Credentials.Gate = "https://api.tenor.com/v1";
|
|
41
|
|
|
Credentials.Filter = Credentials.Filter.toLowerCase();
|
|
42
|
|
|
|
|
43
|
|
|
/**
|
|
44
|
|
|
* Should probably move this elsewhere in the future, not a good
|
|
45
|
|
|
* idea having this much code and heavy-lifting in one place.
|
|
46
|
|
|
*/
|
|
47
|
|
|
let Creds = JSON.stringify(Credentials);
|
|
48
|
|
|
|
|
49
|
|
|
function writeConfig()
|
|
50
|
|
|
{
|
|
51
|
|
|
FS.writeFileSync("tenor_config.json", Creds, function (Error) {
|
|
52
|
|
|
if (Error) throw Error;
|
|
|
|
|
|
|
53
|
|
|
console.log(Colors.bold.green(`# [TenorJS] Changes have been made to the configuration file. Process should be restarted.`));
|
|
|
|
|
|
|
54
|
|
|
process.exit(1);
|
|
|
|
|
|
|
55
|
|
|
});
|
|
56
|
|
|
}
|
|
57
|
|
|
|
|
58
|
|
|
try
|
|
59
|
|
|
{
|
|
60
|
|
|
if (!FS.existsSync("tenor_config.json"))
|
|
61
|
|
|
{
|
|
62
|
|
|
writeConfig();
|
|
63
|
|
|
} else {
|
|
64
|
|
|
FS.readFile("tenor_config.json", "utf8", function (Error, Data) {
|
|
65
|
|
|
if (Error) throw Error;
|
|
|
|
|
|
|
66
|
|
|
if (Data !== Creds) writeConfig();
|
|
|
|
|
|
|
67
|
|
|
});
|
|
68
|
|
|
}
|
|
69
|
|
|
} catch (E) {
|
|
70
|
|
|
throw E;
|
|
71
|
|
|
}
|
|
72
|
|
|
|
|
73
|
|
|
return require("./src")(Credentials);
|
|
74
|
|
|
}; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.